package com.microservice.base.service.impl;

import com.microservice.base.entity.*;
import com.microservice.base.mapper.hikcommon.*;
import com.microservice.base.service.TbPersonSV;
import com.microservice.base.vo.searchVo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jbx
 * @package_name com.microservice.base.service.impl
 * @create_date 2020/12/8 14:10
 */
@Service
public class TbPersonSVI implements TbPersonSV {
  @Autowired
  PersonMP personMP;
  @Autowired
  TreeviewUserMP treeviewUserMP;
  @Autowired UserMapper userMapper;
  @Autowired
  InfoStudentMP infoStudentMP;
  @Autowired
  InfoTeacherMP infoTeacherMP;
  @Autowired
  FaceMP faceMP;

  @Override
  public void save(Person person) {
    personMP.save(person);
  }

  @Override
  public void saveBatch(List<Person> list) {}

  @Override
  public void delete(String id) {}

  @Override
  public void deleteBatch(String[] ids) {}

  @Override
  public int update(Person person) {
    return 0;
  }

  @Override
  public List<Person> query(SearchVo searchVo) {
    return null;
  }

  @Override
  public Person get(String id) {
    return null;
  }

  @Override
  public int getCount(SearchVo searchVo) {
    return 0;
  }

  @Override
  public List<Person> queryAll() {
    List<Person> people = personMP.queryAll();
    return people;
  }

  @Override
  public void savePersonAndFace(User user, String operationType) {
    Person person = new Person();
    Face face = new Face();
    person.setPersonId(user.getUserId());
    person.setPersonName(user.getRealName());
    face.setPersonId(user.getUserId());
    face.setStatus("0".equals(user.getDeleted()) ? 0 : -1);
    face.setUpdateTime(user.getUpdateTime());
    // 可能为空,可能不为空,人脸照片
    face.setFaceUrl(user.getAvatar());
    if ("2".equals(user.getUserType())) {
      // 老师的信息
      InfoTeacher infoTeacher = infoTeacherMP.get(user.getJoinId());
      SysTreeview treeByUseIdAndTvtype = userMapper.getTreeByUseIdAndTvtype(user, "s-d");
      // 未知性别默认为男
      person.setSex("".equals(infoTeacher.getSex()) ? 0 : Integer.parseInt(infoTeacher.getSex()));
      person.setOrgId(treeByUseIdAndTvtype.getId());
      person.setCertType(111);
      person.setCertNum(infoTeacher.getIdNo());
      person.setEmployeeId(infoTeacher.getTeacherCode());
      person.setStatus("0".equals(user.getDeleted()) ? 0 : -1);
      person.setUpdateTime(user.getUpdateTime());
      person.setCardHex(user.getCardHex());
      person.setCardStatus(user.getCardStatus());
      if ("1".equals(operationType)) {
        // 操作类型为1->新增
        person.setOperation(2);
        personMP.save(person);
        if (face.getFaceUrl() != null && !"".equals(face.getFaceUrl())) {
          face.setOperation(2);
          faceMP.save(face);
        }
      } else if ("2".equals(operationType)) {
        // 操作类型为2->更新
        person.setOperation(1);
        personMP.update(person);
        if (faceMP.get(face.getPersonId()) != null) {
          if (face.getFaceUrl() != null && !"".equals(face.getFaceUrl())) {
            face.setOperation(1);
            faceMP.update(face);
          } else {
            // 删除人脸信息
            Face realface = new Face();
            realface.setPersonId(face.getPersonId());
            realface.setOperation(1);
            realface.setStatus(-1);
            faceMP.update(face);
          }
        } else if (face.getFaceUrl() != null
            && !"".equals(face.getFaceUrl())
            && faceMP.get(face.getPersonId()) == null) {
          face.setOperation(2);
          faceMP.save(face);
        }
      }
      user.setSyncFlag("9");
      userMapper.update(user);
    } else if ("3".equals(user.getUserType())) {
      // 学生的信息
      InfoStudent infoStudent = infoStudentMP.get(user.getJoinId());
      SysTreeview treeByUseIdAndTvtype = userMapper.getTreeByUseIdAndTvtype(user, "g-c");
      // 可能存在无组织的记录,不进行person的同步
      person.setSex("".equals(infoStudent.getSex()) ? 0 : Integer.parseInt(infoStudent.getSex()));
      person.setOrgId(treeByUseIdAndTvtype.getId());
      person.setCertType(111);
      person.setCertNum(infoStudent.getIdNo());
      person.setEmployeeId(infoStudent.getStudentCode());
      person.setStatus("0".equals(user.getDeleted()) ? 0 : -1);
      person.setUpdateTime(user.getUpdateTime());
      person.setCardHex(user.getCardHex());
      person.setCardStatus(user.getCardStatus());
      if ("1".equals(operationType)) {
        // 操作类型为1->新增
        person.setOperation(2);
        personMP.save(person);
        // 人脸不为空新增
        if (face.getFaceUrl() != null && !"".equals(face.getFaceUrl())) {
          face.setOperation(2);
          faceMP.save(face);
        }
      } else if ("2".equals(operationType)) {
        // 操作类型为2->更新
        person.setOperation(1);
        personMP.update(person);
        if (faceMP.get(face.getPersonId()) != null) {
          if (face.getFaceUrl() != null && !"".equals(face.getFaceUrl())) {
            face.setOperation(1);
            faceMP.update(face);
          } else {
            // 删除人脸信息
            Face realface = new Face();
            realface.setPersonId(face.getPersonId());
            realface.setOperation(1);
            realface.setStatus(-1);
            faceMP.update(face);
          }
        } else if (face.getFaceUrl() != null
            && !"".equals(face.getFaceUrl())
            && faceMP.get(face.getPersonId()) == null) {
          face.setOperation(2);
          faceMP.save(face);
        }
      }
      user.setSyncFlag("9");
      userMapper.update(user);
    }
    // 其他的,如学校管理员等不进行同步

  }

  @Override
  public Map<Object, Object> queryAllCount() {
    return personMP.queryAllCount();
  }

  @Override
  public void userToPerson(String operationType) {
    // 全量同步
    if (("0").equals(operationType)) {
      // 全量同步的话只需要关注deleted='0'的数据,全量同步即是第一次同步
      List<User> userList = userMapper.queryAll();
      for (User user : userList) {
        SysTreeview treeByUseIdAndTvtype = null;
        if ("2".equals(user.getUserType())) {
          treeByUseIdAndTvtype = userMapper.getTreeByUseIdAndTvtype(user, "s-d");
        } else if ("3".equals(user.getUserType())) {
          treeByUseIdAndTvtype = userMapper.getTreeByUseIdAndTvtype(user, "g-c");
        }
        if (treeByUseIdAndTvtype != null) {
          // 有部门的进行上传,无部门的不上传
          savePersonAndFace(user, "1");
        }
      }
      infoStudentMP.updateSync("9");
      infoTeacherMP.updateSync("9");
      treeviewUserMP.updateSync("9");
    } else {
      // 增量更新,要注意此时不能再只查询deleted='0'的数据,因为可能在第一次全量的时候,他的deleted='0'
      // 但是更新后将其变为不为0,然后将其进行更新
      // 新的问题:用户可能无部门,此处需要增加新的判断
      // 判断逻辑如下:先找到syncFlag=1的user和treeviewuser
      // 如果该用户无部门,则不进行person的同步
      // 如果之前有部门,删除了treeviewuser的部门,进行person数据的删除
      List<User> userLists = userMapper.QueryUserBySyncFlag("1");
      List<TreeviewUser> treeviewUsers = treeviewUserMP.QueryUserTreeBySyncFlag("1");
      List<InfoTeacher> teachers = infoTeacherMP.QueryUserTreeBySyncFlag("1");
      List<InfoStudent> studentList = infoStudentMP.QueryUserTreeBySyncFlag("1");
      if (userLists.size() > 0) {
        List<User> updateUserList = new ArrayList<>();
        for (User user : userLists) {
          updateUserList.add(user);
        }
        // 针对于user的增删改查
        for (User user : updateUserList) {
          Person person = personMP.get(user.getUserId());
          SysTreeview treeByUseIdAndTvtype = null;
          if ("2".equals(user.getUserType())) {
            treeByUseIdAndTvtype = userMapper.getTreeByUseIdAndTvtype(user, "s-d");
          } else if ("3".equals(user.getUserType())) {
            treeByUseIdAndTvtype = userMapper.getTreeByUseIdAndTvtype(user, "g-c");
          }
          if (treeByUseIdAndTvtype != null) {
            // 组织不为空才进行同步
            if (person == null) {
              // 进行新增
              savePersonAndFace(user, "1");
            } else {
              // 进行更新
              savePersonAndFace(user, "2");
            }
          } else if (person != null) {
            // 组织为空,但是person表中有这个人,说明该人的组织遭到了删除,即该人现在没有部门
            // 进行的操作,person表中逻辑删除该数据
            person.setStatus(-1);
            personMP.update(person);
          }
        }
      }
      if (treeviewUsers.size() > 0) {
        // 针对于组织的增删改
        // 如果用户并没有被修改,只修改了treeviewuser,即修改了组织部门
        for (TreeviewUser treeviewUser : treeviewUsers) {
          if (treeviewUser.getUserType() == null
              || "".equals(treeviewUser.getUserType())
              || "0".equals(treeviewUser.getUserType())
              || "1".equals(treeviewUser.getUserType())) {
            // 学校管理员,操作员不进行处理

          } else if (("2".equals(treeviewUser.getUserType())
                  && "s-d".equals(treeviewUser.getTvType()))
              || ("3".equals(treeviewUser.getUserType())
                  && "g-c".equals(treeviewUser.getTvType()))) {
            // 查找person表中有没有该有效用户,没有,则不作处理,有的话进行处理
            Person person = personMP.get(treeviewUser.getUserId());
            if (person != null && "0".equals(treeviewUser.getDeleted())) {
              // 移动组织即组织的更新
              person.setOrgId(treeviewUser.getTreeviewId());
              personMP.update(person);
            } else if (person != null && !"0".equals(treeviewUser.getDeleted())) {
              // 删除组织
              person.setStatus(-1);
              personMP.update(person);
              Face face = new Face();
              face.setPersonId(person.getPersonId());
              face.setStatus(-1);
              faceMP.update(face);
            }
            treeviewUser.setSyncFlag("9");
            treeviewUserMP.update(treeviewUser);
          }
        }
        treeviewUserMP.updateSync("9");
      }
      if (teachers.size() > 0) {
        for (InfoTeacher infoTeacher : teachers) {
          User userByJoinId = userMapper.getUserByJoinId(infoTeacher.getId());
          SysTreeview treeByUseIdAndTvtype =
              userMapper.getTreeByUseIdAndTvtype(userByJoinId, "s-d");
          Person person = new Person();
          if (treeByUseIdAndTvtype != null) {
            person.setOrgId(treeByUseIdAndTvtype.getId());
            person.setCertNum(infoTeacher.getIdNo());
            person.setEmployeeId(infoTeacher.getTeacherCode());
            person.setUpdateTime(infoTeacher.getUpdateTime());
            person.setPersonName(infoTeacher.getName());
            person.setPersonId(userByJoinId.getUserId());
            person.setStatus("0".equals(userByJoinId.getDeleted()) ? 0 : -1);
            person.setSex(
                "".equals(infoTeacher.getSex()) ? 0 : Integer.parseInt(infoTeacher.getSex()));
            person.setOperation(1);
            int flag = personMP.update(person);
            if (flag == 1) {
              infoTeacher.setSyncFlag("9");
              infoTeacherMP.update(infoTeacher);
            }
          }
        }
      }
      if (studentList.size() > 0) {
        for (InfoStudent infoStudent : studentList) {
          User userByJoinId = userMapper.getUserByJoinId(infoStudent.getId());
          SysTreeview treeByUseIdAndTvtype =
              userMapper.getTreeByUseIdAndTvtype(userByJoinId, "g-c");
          Person person = new Person();
          if (treeByUseIdAndTvtype != null) {
            person.setOrgId(treeByUseIdAndTvtype.getId());
            person.setCertNum(infoStudent.getIdNo());
            person.setEmployeeId(infoStudent.getStudentCode());
            person.setUpdateTime(infoStudent.getUpdateTime());
            person.setPersonName(infoStudent.getName());
            person.setPersonId(userByJoinId.getUserId());
            person.setStatus("0".equals(userByJoinId.getDeleted()) ? 0 : -1);
            person.setSex(
                "".equals(infoStudent.getSex()) ? 0 : Integer.parseInt(infoStudent.getSex()));
            person.setOperation(1);
            int flag = personMP.update(person);
            if (flag == 1) {
              infoStudent.setSyncFlag("9");
              infoStudentMP.update(infoStudent);
            }
          }
        }
      }
    }
  }
}
